package com.sky.controller.admin;


import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    //新增地址
    @PostMapping("")
    public Result<Object> add(@RequestBody AddressBook addressBook){
        log.info("地址簿新增数据{}",addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }

    //查询当前登陆用户所有地址
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询用户所有的地址信息");
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    //查询默认地址信息
    @GetMapping("/default")
    public Result<AddressBook> defaultList(){
        log.info("查询默认的地址信息");
        AddressBook list = addressBookService.defaultList();
        return Result.success(list);
    }

    //依据id修改地址
    @PutMapping("")
    public Result<Object> updateById(@RequestBody AddressBook addressBook){
        log.info("依据id修改地址{}",addressBook);
        addressBookService.updateById(addressBook);
        return Result.success();
    }
    //TODO依据id删除地址
    @DeleteMapping("")
    public Result<Object> deleteById(@RequestBody AddressBook addressBook){
        return Result.success();
    }
    //依据id查询地址
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("依据id查询地址信息");
        AddressBook result = addressBookService.getById(id);
        return Result.success(result);
    }

    //设置默认地址
    @PutMapping("/default")
    public Result<Object> updateDefaultById(@RequestBody AddressBook addressBook){
        log.info("设置当前用户的默认地址:{}",addressBook);
        addressBookService.updateDefaultById(addressBook);
        return Result.success();
    }


}
